﻿using System.Collections.Generic;

namespace Sidvall.Serialization
{
    public class DataTypePropertyItemCollection : System.Collections.ObjectModel.KeyedCollection<Sidvall.Data.DataType, DataTypePropertyItem>
    {
        #region Public Members

        #region Add

        public DataTypePropertyItem Add(Sidvall.Data.DataType dataType, IValueProvider valueProvider)
        {
            return Add(dataType, valueProvider, null);
        }
        public DataTypePropertyItem Add(Sidvall.Data.DataType dataType, IValueProvider valueProvider, System.Type propertyType)
        {
            return Add(dataType, valueProvider, propertyType, null);
        }
        public DataTypePropertyItem Add(Sidvall.Data.DataType dataType, IValueProvider valueProvider, System.Type propertyType, int? displayPosition)
        {
            var item = new DataTypePropertyItem()
            {
                DataType = dataType,
                DisplayPosition = displayPosition,
                PropertyType = propertyType,
                ValueProvider = valueProvider,
            };
            Add(item);
            return item;
        }

        public DataTypePropertyItem Add(Sidvall.Data.DataType dataType, IJsonConverter memberConverter)
        {
            return Add(dataType, memberConverter, null);
        }
        public DataTypePropertyItem Add(Sidvall.Data.DataType dataType, IJsonConverter memberConverter, System.Type propertyType)
        {
            return Add(dataType, memberConverter, propertyType, null);
        }
        public DataTypePropertyItem Add(Sidvall.Data.DataType dataType, IJsonConverter memberConverter, System.Type propertyType, int? displayPosition)
        {
            var item = new DataTypePropertyItem()
            {
                DataType = dataType,
                DisplayPosition = displayPosition,
                PropertyType = propertyType,
                MemberConverter = memberConverter,
            };
            Add(item);
            return item;
        }

        #endregion
        #region AddRange

        public void AddRange(IEnumerable<DataTypePropertyItem> items)
        {
            if (items == null)
                return;
            foreach (var item in items)
                Add(item);
        }

        #endregion
        #region GetItem

        public DataTypePropertyItem GetItem(Sidvall.Data.DataType type)
        {
            if (!Contains(type))
                return null;
            else
                return this[type];
        }

        #endregion
        #region GetKeyForItem

        protected override Sidvall.Data.DataType GetKeyForItem(DataTypePropertyItem item)
        {
            if (item == null)
                throw new System.ArgumentNullException("item");
            return item.DataType;
        }

        #endregion

        #endregion
        #region Constructors

        public DataTypePropertyItemCollection()
        {
        }
        public DataTypePropertyItemCollection(IEnumerable<DataTypePropertyItem> items)
        {
            AddRange(items);
        }

        #endregion
    }
}
